/*
  @author: remy sharp / http://remysharp.com
  @url: http://remysharp.com/2008/04/01/wiki-to-html-using-javascript/
  @license: Creative Commons License - ShareAlike http://creativecommons.org/licenses/by-sa/3.0/
  @version: 1.0
  
  Can extend String or be used stand alone - just change the flag at the top of the script.
*/
var book_name = "slide" ;
var chapter_index = 0;
var section_index = 0;
var sub_section_index = 0;
var sub_sub_section_index = 0;
var sub_sub_sub_section_index = 0;
var sub_sub_sub_sub_section_index = 0;
var sub_sub_sub_sub_sub_section_index = 0;
var section_number_str = "";
var toc_html = "";
var topic_depth = 0 ;
(function () {
    
var extendString = true;

if (extendString) {
    String.prototype.wiki2html = wiki2html;
    String.prototype.iswiki = iswiki;
} else {
    window.wiki2html = wiki2html;
    window.iswiki = iswiki;
}

// utility function to check whether it's worth running through the wiki2html
function iswiki(s) {
    if (extendString) {
        s = this;
    }

    return !!(s.match(/^[\s{2} `#\*='{2}]/m));
}

// the regex beast...
function wiki2html(s) {
    if (extendString) {
        s = this;
    }
    
    // lists need to be done using a function to allow for recusive calls
    function list(str) {
        return str.replace(/(?:(?:(?:^|\n)[\*#].*)+)/g, function (m) {  // (?=[\*#])
            var type = m.match(/(^|\n)#/) ? 'OL' : 'UL';
            // strip first layer of list
            m = m.replace(/(^|\n)[\*#][ ]{0,1}/g, "$1");
            m = list(m);
            return '<' + type + '><li>' + m.replace(/^\n/, '').split(/\n/).join('</li><li>') + '</li></' + type + '>';
        });
    }
    
    return list(s
        
        /* BLOCK ELEMENTS */
        .replace(/(?:^|\n+)([^# =\*<].+)(?:\n+|$)/gm, function (m, l) {
            if (l.match(/^\^+$/)) return l;
            return "\n<p>" + l + "</p>\n";
        })

        .replace(/(?:^|\n)[ ]{2}(.*)+/g, function (m, l) { // blockquotes
            if (l.match(/^\s+$/)) return m;
            return '<blockquote>' + l + '</pre>';
        })
        
        .replace(/((?:^|\n)[ ]+.*)+/g, function (m) { // code
            if (m.match(/^\s+$/)) return m;
            return '<pre>' + m.replace(/(^|\n)[ ]+/g, "$1") + '</pre>';
        })

        .replace(/(?:^|\n)([=]+)(.*)\1/g, function (m, l, t) { // headings
        	
        	 
        	switch (l.length){
        	case 1: 
        		chapter_index = chapter_index + 1 ;
        		section_index = 0;
        		sub_section_index = 0;
        		sub_sub_section_index = 0;
        		sub_sub_sub_section_index = 0;
        		sub_sub_sub_sub_section_index = 0;
        		sub_sub_sub_sub_sub_section_index = 0;
        		
        		sub_str = "slide" ;
        		section_number_str = chapter_index + "";
        		
       		
        		
        	break;
        	case 2: 
        		section_index = section_index + 1 ;
        		sub_section_index = 0;
        		sub_sub_section_index = 0;
        		sub_sub_sub_section_index = 0;
        		sub_sub_sub_sub_section_index = 0;
        		sub_sub_sub_sub_sub_section_index = 0;
        		
        		sub_str = "section" ;
        		section_number_str = section_index + "";
        		        		
        		
        	break;
        	case 3: 
        		sub_section_index = sub_section_index + 1;
        		sub_sub_section_index = 0;
        		sub_sub_sub_section_index = 0;
        		sub_sub_sub_sub_section_index = 0;
        		sub_sub_sub_sub_sub_section_index = 0;
        		sub_str = "sub_section" ;
        		section_number_str = 
        			section_index + "." + 
        			sub_section_index;
        		
        		
        	break;
        	case 4: 
           		sub_sub_section_index = sub_sub_section_index + 1;
        		sub_sub_sub_section_index = 0;
        		sub_sub_sub_sub_section_index = 0;
        		sub_sub_sub_sub_sub_section_index = 0;
        		sub_str = "sub_sub_section" ;
        		section_number_str = 
        			section_index + "." + 
        			sub_section_index+ "." +
        			sub_sub_section_index;
        		
        		
        		
        	break;
        	case 5:
        		sub_sub_sub_section_index = sub_sub_sub_section_index +1 ;
        		sub_sub_sub_sub_section_index = 0;
        		sub_sub_sub_sub_sub_section_index = 0;
        		sub_str = "sub_sub_sub_section" ;
        		section_number_str = 
        			section_index + "." + 
        			sub_section_index + "." +
        			sub_sub_section_index + "." + 
        			sub_sub_sub_section_index;
        		
        		
        		
        	break;
        	case 6:
        		sub_sub_sub_sub_section_index = sub_sub_sub_sub_section_index + 1 ;
        		sub_sub_sub_sub_sub_section_index = 0;
        		sub_str = "sub_sub_sub_sub_section" ;
        		section_number_str = 
        			section_index + "." + 
        			sub_section_index + "." + 
        			sub_sub_section_index + "." +        		
        			sub_sub_sub_section_index +"." + 
        			sub_sub_sub_sub_section_index;
        		
        		
        		
        	break;
        	case 7: 
        		sub_sub_sub_sub_sub_section_index = sub_sub_sub_sub_sub_section_index + 1;
        		
        		sub_str = "sub_sub_sub_sub_sub_section" ;
        		section_number_str = 
        			section_index + "." + 
        			sub_section_index + "." +        		
        			sub_sub_section_index + "." +
        			sub_sub_sub_section_index + "." + 
        			sub_sub_sub_sub_section_index + "." + 
        			sub_sub_sub_sub_sub_section_index;
        		
        		
        		
        	break;
        	default : alert("I\'m sure it was great");
        	}

        	if (l.length == 1){
        		LI = "<li><a href=\"javascript:fun_goto_slide("+chapter_index+") ; hide_toc(); \">" + t + "</a></li>" ;	
            }else{
            	LI = "<li><a href=\"#"+book_name + "_" + chapter_index + "." + section_number_str  +  "\" > " + ((section_number_str.length + 1 ) / 2 ) + t + "</a></li>" ;
            }
        	
        	
        	if(topic_depth < l.length ){
        		Start_ol_str = "";
    			for (var is_td = 0 ; is_td < (l.length - topic_depth) ; is_td++  ){
    				Start_ol_str +=  "<ol>"
    			}
    			
        		toc_html = toc_html + Start_ol_str + LI ;}
    		if(topic_depth == l.length ){toc_html = toc_html + LI;}
    		if(topic_depth > l.length ){
    			Close_ol_str = "";
    			for (var i_td = 0 ; i_td < (topic_depth - l.length ) ; i_td++  ){
    				Close_ol_str +=  "</ol>"
    			}
    			toc_html = toc_html + Close_ol_str + LI;
    			
    		}
    		
        	topic_depth = l.length ;
        	
        	if (l.length == 1){
        		if(chapter_index == 1){
        	return "<div id=\"slide" + chapter_index + "\" class=\""+ sub_str +"\" title=\"" + t + "\" label=\"chap_intro\"> " + "<h" + l.length + ">" + chapter_index + ". " +t + "</h" + l.length + ">"
        		}
        		else{
        	return "</div><div id=\"slide" + chapter_index + "\" class=\""+ sub_str +"\" title=\"" + t + "\" label=\"chap_intro\"> " + "<h" + l.length + ">" + chapter_index + ". " + t + "</h" + l.length + ">"        			
        		}
        	
        	}else{
        	return "<div id=\""+book_name + "_" + chapter_index + "." + section_number_str +"\" class=\""+ sub_str +"\" title=\"" + t + "\" label=\"chap_intro\"> " + "<h" + l.length + ">" +  t + "</h" + l.length + ">" +" </div>"
        	}
        	
        })
    
        /* INLINE ELEMENTS */
        .replace(/'''(.*?)'''/g, function (m, l) { // bold
            return '<strong>' + l + '</strong>';
        })
    
        .replace(/''(.*?)''/g, function (m, l) { // italic
            return '<em>' + l + '</em>';
        })
    
        .replace(/[^\[](http[^\[\s]*)/g, function (m, l) { // normal link
            return '<a href="' + l + '">' + l + '</a>';
        })
    
        .replace(/[\[](http.*)[!\]]/g, function (m, l) { // external link
            var p = l.replace(/[\[\]]/g, '').split(/ /);
            var link = p.shift();
            return '<a href="' + link + '">' + (p.length ? p.join(' ') : link) + '</a>';
        })
    
        .replace(/\[\[(.*?)\]\]/g, function (m, l) { // internal link or image
            var p = l.split(/\|/);
            var link = p.shift();

            if (link.match(/^Image:(.*)/)) {
                // no support for images - since it looks up the source from the wiki db :-(
                return m;
            } else {
                return '<a href="' + link + '">' + (p.length ? p.join('|') : link) + '</a>';
            }
        })
    ); 
}
    
})();